﻿Imports System.ComponentModel

Public Class VM_MobileProvider
	Implements IDisposable

	Dim db As New CongregationDataContainerLocal
	Dim workingProvider As MobileProvider

	Public ReadOnly Property ProviderID As Long
		Get
			Return workingProvider.ProviderID
		End Get
	End Property

	Public Property ServiceProvider As String
		Get
			Return workingProvider.ServiceProvider
		End Get
		Set(value As String)
			workingProvider.ServiceProvider = value
			db.SaveChanges()
		End Set
	End Property

	Public Property ServiceProviderTextMessageEmailDomain As String
		Get
			Return workingProvider.ProviderTextMessageEmailDomain
		End Get
		Set(value As String)
			workingProvider.ProviderTextMessageEmailDomain = value
			db.SaveChanges()
		End Set
	End Property

	Public Sub New(Optional ProviderID As Long? = Nothing)

		If ProviderID Is Nothing Then
			workingProvider = New MobileProvider
			workingProvider.ServiceProvider = "New Provider"
			workingProvider.ProviderTextMessageEmailDomain = "newdomain.com"
			db.MobileProviders.AddObject(workingProvider)
			db.SaveChanges()
		Else
			workingProvider = db.MobileProviders.SingleOrDefault(Function(x) x.ProviderID = ProviderID)

			If workingProvider Is Nothing Then
				Throw New ArgumentOutOfRangeException("A provider with that ID doesn't exist.")
			End If
		End If

	End Sub

	Public Shared ReadOnly Property AllMobileProviders As BindingList(Of VM_MobileProvider)
		Get
			Dim returnList As New BindingList(Of VM_MobileProvider)

			Using db As New CongregationDataContainerLocal
				For Each provider In db.MobileProviders
					returnList.Add(New VM_MobileProvider(provider.ProviderID))
				Next
			End Using
			Return returnList
		End Get
	End Property

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
